// DlgOutputProperty.cpp : implementation file
//

#include "stdafx.h"
#include "MotionControlSoftware.h"
#include "DlgOutputProperty.h"
#include "afxdialogex.h"


// CDlgOutputProperty dialog

IMPLEMENT_DYNAMIC(CDlgOutputProperty, CDialogEx)

CDlgOutputProperty::CDlgOutputProperty(CWnd* pParent /*=NULL*/)
	: CDialogEx(CDlgOutputProperty::IDD, pParent)
	, m_bEnablePulse(FALSE)
	, m_iPulseTime(0)
	, m_iPort(0)
	, m_bEnablePosMode(FALSE)
	, m_bEnableOutH(FALSE)
{
	m_pOutputSignal = NULL;
	m_OutputSignalBlock = NULL;
}

CDlgOutputProperty::~CDlgOutputProperty()
{
}

void CDlgOutputProperty::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Check(pDX, IDC_CHECK_EnableOutputPulse, m_bEnablePulse);
	DDX_Text(pDX, IDC_EDIT_OutputPulseTime, m_iPulseTime);
	DDV_MinMaxInt(pDX, m_iPulseTime, 0, 1000000);
	DDX_Text(pDX, IDC_EDIT_OutPortNO, m_iPort);
	DDV_MinMaxInt(pDX, m_iPort, 0, 255);
	DDX_Check(pDX, IDC_CHECK_OutEnablePosMode, m_bEnablePosMode);
	DDX_Check(pDX, IDC_CHECK_EnableOutH, m_bEnableOutH);
}


BEGIN_MESSAGE_MAP(CDlgOutputProperty, CDialogEx)
	ON_WM_SHOWWINDOW()
	ON_BN_CLICKED(IDC_CHECK_EnableOutputPulse, &CDlgOutputProperty::OnBnClickedCheckEnableoutputpulse)
END_MESSAGE_MAP()


// CDlgOutputProperty message handlers


BOOL CDlgOutputProperty::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  Add extra initialization here
	if (m_OutputSignalBlock != NULL)
	{
		CGraph* pGraph = m_OutputSignalBlock->GetHead();
		m_pOutputSignal = (COutputSignal*)(pGraph->GetHead());
		if (m_pOutputSignal->m_iType == 0)
		{
			m_bEnablePulse = FALSE; 
		}
		else
		{
			m_bEnablePulse = TRUE; 
		}

		m_iPort = m_pOutputSignal->m_iPort;
		m_iPulseTime = m_pOutputSignal->m_iDelayTime;

		GetDlgItem(IDC_EDIT_OutputPulseTime)->EnableWindow(m_bEnablePulse);

		if (m_pOutputSignal->m_iPosMode == 0)
		{
			m_bEnablePosMode = FALSE;
		}
		else
		{
			m_bEnablePosMode = TRUE;
		}
		((CButton*)GetDlgItem(IDC_CHECK_InEnablePosMode))->SetCheck(m_bEnablePosMode);

		if (m_pOutputSignal->m_iOutSignalType == 1)
		{
			m_bEnableOutH = TRUE;
		}	
		else
		{
			m_bEnableOutH = FALSE;
		}
		((CButton*)GetDlgItem(IDC_CHECK_EnableOutH))->SetCheck(m_bEnableOutH);
	}

	UpdateData(FALSE);
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

bool CDlgOutputProperty::UpdateParam()
{
	if (UpdateData())
	{
		if (m_pOutputSignal != NULL)
		{
			m_pOutputSignal->m_iDelayTime = m_iPulseTime;
			if (m_bEnablePulse)
			{
				m_pOutputSignal->m_iType = 1;
			}
			else
			{
				m_pOutputSignal->m_iType = 0;
			}
			m_pOutputSignal->m_iPort = m_iPort;
			if (m_bEnablePosMode)
			{
				m_pOutputSignal->m_iPosMode = 1;
			}
			else
			{
				m_pOutputSignal->m_iPosMode = 0;
			}

			if (m_bEnableOutH)
			{
				m_pOutputSignal->m_iOutSignalType = 1;
			}
			else
			{
				m_pOutputSignal->m_iOutSignalType = 0;
			}
			return true;
		}
	}
	return false;
}


void CDlgOutputProperty::OnShowWindow(BOOL bShow, UINT nStatus)
{
	CDialogEx::OnShowWindow(bShow, nStatus);

	// TODO: Add your message handler code here
	if (m_OutputSignalBlock != NULL)
	{
		CGraph* pGraph = m_OutputSignalBlock->GetHead();
		m_pOutputSignal = (COutputSignal*)(pGraph->GetHead());
		if (m_pOutputSignal->m_iType == 0)
		{
			m_bEnablePulse = FALSE; 
		}
		else
		{
			m_bEnablePulse = TRUE; 
		}

		m_iPort = m_pOutputSignal->m_iPort;
		m_iPulseTime = m_pOutputSignal->m_iDelayTime;

		
		GetDlgItem(IDC_EDIT_OutputPulseTime)->EnableWindow(m_bEnablePulse);
		
		if (m_pOutputSignal->m_iPosMode == 0)
		{
			m_bEnablePosMode = FALSE;
		}
		else
		{
			m_bEnablePosMode = TRUE;
		}
		((CButton*)GetDlgItem(IDC_CHECK_InEnablePosMode))->SetCheck(m_bEnablePosMode);

		if (m_pOutputSignal->m_iOutSignalType == 1)
		{
			m_bEnableOutH = TRUE;
		}	
		else
		{
			m_bEnableOutH = FALSE;
		}
		((CButton*)GetDlgItem(IDC_CHECK_EnableOutH))->SetCheck(m_bEnableOutH);
	}

	UpdateData(FALSE);
}


void CDlgOutputProperty::OnBnClickedCheckEnableoutputpulse()
{
	// TODO: Add your control notification handler code here
	if (UpdateData(TRUE))
	{
		GetDlgItem(IDC_EDIT_OutputPulseTime)->EnableWindow(m_bEnablePulse);
	}
}
